/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Examples;

import homomorphism.list.ThirdHomomorphismTheoremEx;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.hadoop.io.LongWritable;
import utility.LongPair;
import utility.PairT;

/**
 *
 * @author liuyu
 */
public class Mps2 extends ThirdHomomorphismTheoremEx<LongWritable, LongPair>{

    public static LongPair mps_sum(Iterator<LongWritable> xs) {
        Long mps = 0l;
        Long sum = 0l;
        while (xs.hasNext()) {
            sum += xs.next().get();
            if (sum > mps) {
                mps = sum;
            }
        }
        return new LongPair(mps, sum);
    }

    public ArrayList<LongWritable> unfold(LongPair value) {
        ArrayList<LongWritable> rst = new ArrayList<LongWritable>();
        long tmp = value.getFirst();
        rst.add(new LongWritable(tmp));
        long tmp2 = (value.getSecond() - tmp);
        rst.add(new LongWritable(tmp2));
        return rst;
    }

    @Override
    public LongPair fold(Iterator<LongWritable> values) {
        return mps_sum(values);
    }

        public static PairT<Long> mps_sum(ArrayList<LongWritable> xs) {
        Long mps = 0l;
        Long sum = 0l;
        for (LongWritable v : xs) {
            sum += v.get();
            if (sum > mps) {
                mps = sum;
            }
        }
        return new PairT<Long>(mps, sum);
    }

    public LongPair fold(ArrayList<LongWritable> values) {
//        PairT<Long> mps_sum = mps_sum(values);
//        LongPair rst = new LongPair(mps_sum);
//        return rst;
        return(new LongPair(mps_sum(values)));
    }
    
}
